package com.dgshanger.sanhxiaofeisc.ui.home.activity

import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.LinearLayout
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.LoosePaymentEntity
import com.dgshanger.sanhxiaofeisc.entity.PaymentCodeEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.PaymentCodeContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.PaymentCodePresenter
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.dgshanger.sanhxiaofeisc.ui.person.setting.EditPayPasswordActivity
import com.dgshanger.sanhxiaofeisc.ui.recharge.PayForActivity
import com.xx.baseuilibrary.mvp.lcec.BaseMvpLcecActivity
import com.xxl.sodmall.dialog.WalletPayDialogWrapper
import kotlinx.android.synthetic.main.activity_pay_model_setting.*
import kotlinx.android.synthetic.main.activity_payment_code.*

/**
 * Created by JacobHHH on 2018/4/21.
 */
class PaymentCodeActivity : BaseMvpLcecActivity<LinearLayout, PaymentCodeEntity, PaymentCodeContract.Model, PaymentCodeContract.View, PaymentCodePresenter>(), PaymentCodeContract.View, View.OnClickListener {

    private lateinit var mShop_number: String
    private lateinit var mData: PaymentCodeEntity

    override fun createPresenter(): PaymentCodePresenter = PaymentCodePresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_payment_code

    override fun initData() {
        super.initData()
        mShop_number = intent.getStringExtra("shop_number")
        intView()
        loadData(true)
    }

    private fun intView() {
        tv_pays.setOnClickListener(this)
        rl_loose.setOnClickListener(this)
        rl_other.setOnClickListener(this)
    }

    override fun initEvent() {
        ed_payment.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(edt: Editable?) {
                val temp = edt.toString()
                val posDot = temp.indexOf(".")
                if (posDot <= 0) return
                if (temp.length - posDot - 1 > 2) {
                    edt!!.delete(posDot + 3, posDot + 4)
                }
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

        })
        ed_integral.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(edt: Editable?) {
                val temp = edt.toString()
                val posDot = temp.indexOf(".")
                if (posDot <= 0) return
                if (temp.length - posDot - 1 > 2) {
                    edt!!.delete(posDot + 3, posDot + 4)
                }
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

        })
    }

    override fun onClick(view: View?) {
        when (view!!.id) {
            R.id.tv_pays -> {
                /* val intent = Intent(mContext, PayResultActivity::class.java)
                 startActivity(intent)*/
            }
            R.id.rl_loose -> { //零钱支付
                val price = ed_payment.text.toString().trim()
                val integral = ed_integral.text.toString().trim()

                if (TextUtils.isEmpty(price)) {
                    showToast(getString(R.string.Please_enter_payment_amount))
                    return
                }
//
//                if (TextUtils.isEmpty(price) && TextUtils.isEmpty(integral)) {
//                    showToast(getString(R.string.Please_enter_payment_amount_or_gift_certificate))
//                    return
//                } else if ("0.00" == price && "0.00" == integral) {
//                    showToast(getString(R.string.Please_enter_payment_amount_or_gift_certificate))
//                    return
//                } else if ("0.0" == price && "0.0" == integral) {
//                    showToast(getString(R.string.Please_enter_payment_amount_or_gift_certificate))
//                    return
//                }
//                presenter.checkPayPwd()
                val intent = Intent(mContext, PayModelSettingActivity::class.java)
                intent.putExtra("PaymentCodeEntity",mData)
                intent.putExtra("price",price)
                startActivity(intent)
            }
            R.id.rl_other -> {//他人支付
                val price = ed_payment.text.toString().trim()
                if (TextUtils.isEmpty(price) || "0.00" == price || "0.0" == price) {
                    showToast("请输入支付金额")
                    return
                }
                val intent = Intent(mContext, PayForActivity::class.java)
                intent.putExtra("otherpay_shop_id", mShop_number)
                intent.putExtra("price", price)
                startActivity(intent)
                finish()
            }
        }
    }

    override fun loadData(refresh: Boolean) {
        presenter.loadData(refresh, mShop_number)
    }

    override fun setData(data: PaymentCodeEntity?) {
        showContent()
        mData = data!!
        tv_name.text = data!!.shop_name
        ImageLoader.loadToUrl(mContext, iv_shop, Constants.getBaseUrl() + data.menu_img)
    }

    override fun onDataFailure(msg: String?) {
        showToast(msg)
        finish()
    }

    private var mPayType: String = "pay_wallet"
    override fun showPayPwdDialog() {
        var price = ed_payment.text.toString().trim()
        val integral = ed_integral.text.toString().trim()
        if (TextUtils.isEmpty(price)) {
            price = "0.00"
        }
        val walletPayDialogWapper = WalletPayDialogWrapper(mContext, price)
        if (TextUtils.isEmpty(integral)) {
            walletPayDialogWapper.setIntegralText("0.00")
        } else {
            walletPayDialogWapper.setIntegralText(integral)
        }

        walletPayDialogWapper.setOnPasswordFinishListener(object : WalletPayDialogWrapper.OnPasswordFinishListener {
            override fun onPasswordFinishListener(password: String) {

                getPresenter().goPay(mData.show_id, ed_payment.text.toString().trim(), mPayType, password
                        , price, integral)
                walletPayDialogWapper.dismissDialog()
            }
        })
        walletPayDialogWapper.showDialog()
    }

    override fun onSettingPayPwd() {
        val intent = Intent(mContext, EditPayPasswordActivity::class.java)
        startActivity(intent)
    }

    override fun onPaySuccess(entity: LoosePaymentEntity?) {
        val intent = Intent(mContext, PayResultActivity::class.java)
        intent.putExtra("orderId", entity!!.order_sn)
        startActivity(intent)
    }
}